package com.itheima.controller;

import com.itheima.Service.ReportService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.BusinessReportData;
import com.itheima.pojo.Portdev;
import com.itheima.pojo.Protdecsetmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@Slf4j
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private ReportService   reportService;
    /**
     * 获取会员数量统计数据
     */
    @GetMapping("/getMemberReport")
    public Result<Portdev> list(){
        try {
            log.info("统计会员人数");
            Portdev portdev=reportService.list();
            return new Result<>(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,portdev);
        } catch (Exception e) {
            return new Result<>(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL,null);
        }
    }
    /**
     * 获取套餐占比统计数据
     */
    @GetMapping("/getSetmealReport")
    public Result<Protdecsetmeal> setmeallist(){
        try {
            log.info("统计套餐占比数量");
            Protdecsetmeal protdecsetmeal=reportService.setlist();
            return new Result<>(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,protdecsetmeal);
        } catch (Exception e) {
            return new Result<>(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL,null);
        }
    }
    /**
     * 统计运营数据
     */
    @GetMapping("/getBusinessReportData")
    public Result<BusinessReportData> read(){
        try {
            log.info("获取运营数据");
            BusinessReportData businessReportData=reportService.read();
            return new Result<>(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,businessReportData);
        } catch (Exception e) {
            return new Result<>(false,MessageConstant.GET_BUSINESS_REPORT_FAIL,null);

        }
    }
    /**
     * 导出运营数据到Excel并提供客户端下载
     */
    @GetMapping("/exportBusinessReport")
    public Result exclelist(HttpServletResponse response){
        try {
            log.info("模板导出");
            reportService.exclelist(response);
            return new Result(true,"文件下载成功",null);
        } catch (Exception e) {
            return new Result(false,"文件下载失败",null);
        }
    }
}
